/** @file SpawnCommand
    @class SpawnCommand
    @brief Command that spawns a new player avatar
*/
#ifndef __SpawnCommand_h_
#define __SpawnCommand_h_

#include "Command.h"
#include <Windows.h>
class SpawnCommand : public Command {
public:
	/**
	* The constructor
	*/
	SpawnCommand (float x, float y, float z,Ogre::Quaternion orientation, unsigned int playerId, bool foo)
		: Command(playerId) {
		posX = x;
		posY = y;
		posZ = z;
		myAvatar = foo;
		playerOrientation = orientation;
	}
	
	/**
	* The empty constructor
	*/
	SpawnCommand() {}
	
	/**
	* The deconstructor
	*/
	virtual ~SpawnCommand (void) {}
	
	/**
	* Executs the command on argument model
	*/
	virtual bool execute(Model *model) {
		PlayerAvatar *gameObject = new PlayerAvatar(Ogre::Vector3(posX, posY, posZ));
		model->addGameObject(gameObject, getPlayerId(), posX, posY, posZ);
		if ( gameObject == NULL) {
			return false;
		} else {
			gameObject->setPosition(Ogre::Vector3(posX,posY,posZ));
			gameObject->getSceneNode()->setOrientation(playerOrientation);
			return true;
		}
	}
	
	/**
	* Returns if this is the current users avatar
	*/
	bool isMyAvatar() {
		return myAvatar;
	}
	
	/**
	* Saves the command to a output file stream
	*/
	virtual void save(ofstream &ofs) {
		int i = SPAWNCOMMAND;
		ofs.write((char*) &i, sizeof(int));
		Command::save(ofs);
		ofs.write((char*) &posX, sizeof(float));
		ofs.write((char*) &posY, sizeof(float));
		ofs.write((char*) &posZ, sizeof(float));
		ofs.write((char*) &myAvatar, sizeof(bool));
		ofs.write((char*) &playerOrientation, sizeof(Ogre::Quaternion));
  }

	/**
	* Loads a command from a input file stream
	*/
  virtual void load(ifstream &ifs) {
		Command::load(ifs);
		ifs.read((char*) &posX, sizeof(float));
		ifs.read((char*) &posY, sizeof(float));
		ifs.read((char*) &posZ, sizeof(float));
		ifs.read((char*) &myAvatar, sizeof(bool));
		ifs.read((char*) &playerOrientation, sizeof(Ogre::Quaternion));
  }

	/**
	* Saves the command to a bit stream
	*/
  virtual void save(RakNet::BitStream &ofs) {
		int i = SPAWNCOMMAND;
		ofs.Write((char*) &i, sizeof(int));
		Command::save(ofs);
		ofs.Write((char*) &posX, sizeof(float));
		ofs.Write((char*) &posY, sizeof(float));
		ofs.Write((char*) &posZ, sizeof(float));
		ofs.Write((char*) &myAvatar, sizeof(bool));
		ofs.Write((char*) &playerOrientation, sizeof(Ogre::Quaternion));
  }

	/**
	* Loads a command from a bit stream
	*/
  virtual void load(RakNet::BitStream &ifs) {
		Command::load(ifs);
		ifs.Read((char*) &posX, sizeof(float));
		ifs.Read((char*) &posY, sizeof(float));
		ifs.Read((char*) &posZ, sizeof(float));
		ifs.Read((char*) &myAvatar, sizeof(bool));
		ifs.Read((char*) &playerOrientation, sizeof(Ogre::Quaternion));
  }

private:
	/**
	* Spawning position x
	*/
	float posX;
	
	/**
	* Spawning postion y
	*/
	float posY;
	
	/**
	* Spawning position z
	*/
	float posZ;
	
	/**
	* Says if the avatar created is for the current user or some other player
	*/
	bool myAvatar;
	
	/**
	* Spawning orientation
	*/
	Ogre::Quaternion playerOrientation;
};
#endif